package com.goods.shop.exam;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class EX004 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> inputString= new ArrayList<>();
        while (scanner.hasNext()) {
            String tmp = scanner.nextLine();
            if (tmp.trim().equals("-1")) {
                break;
            }
            inputString.add(tmp);
        }
        int num =  Integer.parseInt(inputString.get(0));
        List<String> jigouList = new ArrayList<String>();
        for(int i =0;i<num;i++){
            jigouList.add(inputString.get(i+1));
        }

        String cmd[] = inputString.get(num+1).split(" ");
        String del =cmd[0];
        String add =cmd[1];
        List<String> result = new ArrayList<String>();
        String strAdd[] = new String[5];
        for(int i =0;i<jigouList.size();i++){
            String strs[] = jigouList.get(i).split(" ");
            if(strs[0].equals(add)){
                strAdd = strs;
            }
        }
        for(int i =0;i<jigouList.size();i++){
            String strs[] = jigouList.get(i).split(" ");
            if(strs[0].equals(add)){
                strAdd = strs;
            }
            if(strs[0].equals(del)){
                strs[4] = "1";
            }
            if(strs[2].equals(del) ){
                strs[2] = add;
                String endName=strs[3].split("分行")[1];
//                System.out.println("endName==="+endName);
                strs[3] = strAdd[3]+endName;
            }
            result.add(strs[0]+" "+strs[1]+" "+strs[2]+" "+strs[3]+" "+strs[4]);
        }

        for(int i=0 ;i<result.size();i++){
            if(i != result.size()-1){
                System.out.println(result.get(i).trim());
            }else{
                System.out.print(result.get(i).trim());
            }
        }
    }
}
